//
// Created by 马皓然 on 2025/10/28.
//
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "can_bsp.h"
#include "cmsis_os2.h"
#include "stm32f4xx_hal.h"
#include <stdio.h>
#include "main.h"

static uint32_t tx_counter = 0;

/**
 * @brief  默认任务：系统初始化后进入的测试或最低优先级任务
 *
 * @param  argument 任务创建时传入的参数（通常为 NULL 或特定数据结构指针）
 *
 * @return 无
 *
 * @note   每100ms翻转一次LED灯，作为系统运行的心跳指示
 * @see    osThreadNew
 * @author stm32小高手
 * @date   2025/10/28
 */
void StartDefaultTask(void *argument)
{

    /* USER CODE BEGIN StartDefaultTask */
    CAN_Frame_t tx_frame;
    const TickType_t xDelay200ms = pdMS_TO_TICKS(200);

    tx_frame.IDE = CAN_ID_EXT;
    tx_frame.RTR = CAN_RTR_DATA;
    tx_frame.DLC = 8;
    tx_frame.ID  = 0x12345678;
    /* Infinite loop */
    for(;;)
    {
        tx_counter++;
        tx_frame.Data[0] = (uint8_t)(tx_counter>>24);
        tx_frame.Data[1] = (uint8_t)(tx_counter>>16);
        tx_frame.Data[2] = (uint8_t)(tx_counter>>8);
        tx_frame.Data[3] = (uint8_t)(tx_counter);
        tx_frame.Data[4] = 0xAA;
        tx_frame.Data[5] = 0xBB;
        tx_frame.Data[6] = 0xCC;
        tx_frame.Data[7] = 0xDD;

        if (CAN_Driver_Transmit(&tx_frame) == 0) {
            printf("Transmit ERROR");   // 发送失败处理（可选）
        }


        HAL_GPIO_TogglePin(GPIOF,GPIO_PIN_9);
        osDelay(xDelay200ms);
    }
    /* USER CODE END StartDefaultTask */
}
/**
 * @brief  测试接收任务：阻塞等待数据，收到后立即回发。
 *
 * @note   在任务中安全地执行接收和回发操作。
 */
void Can_Test_Rece(void *argument)
{
    /* USER CODE BEGIN Can_Test_Rece */
    CAN_Frame_t rx_frame;
    CAN_Frame_t tx_response;
    /* Infinite loop */
    for(;;)
    {
        osDelay(1);
    }
    /* USER CODE END Can_Test_Rece */
}